package com.unicom.projt.fee;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.unicom.projt.Home;
import com.unicom.projt.InputPhoneNumberPage;
import com.unicom.projt.Main;
import com.unicom.projt.R;
import com.unicom.projt.ivr.BindCardPage;
import com.unicom.projt.query.QueryCurrentMonthPage;
import com.unicom.projt.query.QueryHistoryPage;
import com.unicom.projt.query.QueryPaymentPage;
import com.unicom.projt.util.AlertDialogUtils;
import com.unicom.projt.util.PageJumpUtils;
import com.unicom.projt.util.SystemUtils;
import com.unicom.projt.util.UserHolder;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-6
 * Time: 下午1:26
 */
public class PaymentSuccessPage extends Activity implements View.OnClickListener {
    private double amount = 0;
    private String phoneNumber;
    private TextView mPayAmount;
    private TextView mPhoneNumber;

    private Handler mHandler;
    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 19;
    private PaymentQueryController mController;

    private String orderId="";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.payment_success);
        ((TextView)this.findViewById(R.id.payment_confirm_number_txt)).setText(UserHolder.phoneNumber);
        mController=new PaymentQueryController(this);
        Bundle extras = getIntent().getExtras();
        amount = extras.getDouble("amount");
        phoneNumber = extras.getString("phoneNumber");
        orderId = extras.getString("orderId");
//        amount=100D;
//        phoneNumber=UserHolder.phoneNumber;

        mPayAmount = (TextView) this.findViewById(R.id.payment_success_amount);

        mPayAmount.setText(amount + "");

        mPhoneNumber = (TextView) this.findViewById(R.id.payment_success_phone_number);
        mPhoneNumber.setText(phoneNumber);

        this.findViewById(R.id.payment_confirm_back_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_confirm_logout_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_back_to_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_success_bind_btn).setOnClickListener(this);

        if(UserHolder.isBindUser){
            this.findViewById(R.id.payment_success_icon).setVisibility(View.GONE);
            this.findViewById(R.id.payment_success_line_1).setVisibility(View.GONE);
            this.findViewById(R.id.payment_success_line_2).setVisibility(View.GONE);
            this.findViewById(R.id.payment_success_line_3).setVisibility(View.GONE);
            this.findViewById(R.id.payment_success_bind_btn).setVisibility(View.GONE);
        }

        mHandler = new Handler();
//        queryCanUseAmount();

    }



    private void queryCanUseAmount() {
        showDialog(DIALOG_PROGRESS);
        mController.queryPayment(UserHolder.phoneNumber);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.payment_confirm_back_btn:
                PageJumpUtils.jump(this, Home.class);
                this.finish();
                break;
            case R.id.payment_back_to_btn:
                PageJumpUtils.jump(this, Home.class);
                this.finish();
                break;
            case R.id.payment_confirm_logout_btn:
                AlertDialogUtils.showYesNoDiaLog(this, "是否注销？", new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        UserHolder.clean();
                        doLogout();
                    }
                });
                break;
            case R.id.payment_success_bind_btn:
                PageJumpUtils.jump(this, BindCardPage.class);
                this.finish();
                break;
        }
    }
    private void doLogout(){
        Intent intent = new Intent(this, InputPhoneNumberPage.class);
        this.startActivity(intent);
        this.finish();
    }

    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Home.class);
        this.finish();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                mDialog.setCancelable(false);
                mDialog.setOnKeyListener(new DialogInterface.OnKeyListener(){
                    public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                            return true;
                        }
                        return false;
                    }
                });
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
